package com.beiming.moudle.auth.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beiming.common.utils.Result;
import com.beiming.moudle.auth.entity.BussEntity;
import com.beiming.moudle.auth.entity.OrderEntity;
import com.beiming.moudle.auth.entity.utillClass.BussPage;
import com.beiming.moudle.auth.entity.utillClass.OrderPage;
import com.beiming.moudle.auth.service.BussService;
import com.beiming.moudle.auth.service.CustomerService;
import com.beiming.moudle.auth.service.OrderService;
import com.beiming.moudle.auth.service.ProInfoService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单信息
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderService orderService;
    private final BussService bussService;
    private final ProInfoService proInfoService;
    private final CustomerService customerService;

    public OrderController(OrderService orderService, BussService bussService, ProInfoService proInfoService, CustomerService customerService) {
        this.orderService = orderService;
        this.bussService = bussService;
        this.proInfoService = proInfoService;
        this.customerService = customerService;
    }

    @PostMapping("/info")
    public Result getPage(@RequestBody OrderPage pa){
        IPage<OrderEntity> orderPage = orderService.getMsgPage(pa);
        //如果当前页码值大于了总页码值，那么重新执行查询操作，使用最大页码值作为当前页码值
        if( pa.getCurrentPage() > orderPage.getPages()){
            pa.setCurrentPage((int)orderPage.getPages());
            orderPage = orderService.getMsgPage(pa);
        }
        List<OrderEntity> orderEntityList = orderPage.getRecords();
        for(OrderEntity orderEntity : orderEntityList){
            orderEntity.setBussinessName(bussService.getBussUser(orderEntity.getBussinessId()));
            orderEntity.setCustomerName(customerService.getCustmoerName(orderEntity.getCustomerId()));
            orderEntity.setProductName(proInfoService.getProName(orderEntity.getProductId()));
            //bussEntity.setBussinessAccountName(bussUserService.getBussLoginUser(bussEntity.getBussinessId()));
        }
        return Result.ok(orderPage);
    }

}
